package com.mtnz.njk.Service;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.mtnz.njk.Mapper.BannerManagerMapper;
import com.mtnz.njk.Mapper.BannerMapper;
import com.mtnz.njk.Mapper.ProductMapper;
import com.mtnz.njk.Mapper.ResearchManageMapper;
import com.mtnz.njk.entity.BannerEntity;
import com.mtnz.njk.entity.ProductEntity;
import com.mtnz.njk.entity.ResearchEntity;
import com.mtnz.njk.result.PageInfo;
import com.mtnz.njk.result.ResponseMessage;
import com.mtnz.njk.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: 首页管理服务层
 * @Author: FBL
 * @CreateDate: 2019\8\26 0026 10:12
 * @Version: 1.0
 */
@Service
public class BannerManagerService {

    @Autowired
    BannerManagerMapper bannerManagerMapper;

    @Autowired
    BannerMapper bannerMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    ResearchManageMapper researchManageMapper;

    /**
     * 首页管理列表展示
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    public ResponseResult listBanner(Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        // 查询商城首页轮播图
        List<BannerEntity> bannerEntities = bannerMapper.selectAll();
        PageInfo<BannerEntity> bannerEntityPageInfo = new PageInfo<>(bannerEntities);

        // 所有已上架商品
        Example productExample = new Example(ProductEntity.class);
        productExample.createCriteria().andEqualTo("soldFlag",0);
        List<ProductEntity> productEntities = productMapper.selectByExample(productExample);

        // 所有已发布帖子
        Example researchExample = new Example(ResearchEntity.class);
        researchExample.createCriteria().andEqualTo("publish",1);
        List<ResearchEntity> researchEntities = researchManageMapper.selectByExample(researchExample);

        HashMap<String, Object> mm = new HashMap<>();
        mm.put("banner",bannerEntityPageInfo);
        mm.put("product",productEntities);
        mm.put("research",researchEntities);

        return new ResponseResult(ResponseMessage.OK, mm);
    }

    /**
     * 首页管理列表编辑
     *
     * @param bannerEntity
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult updateBanner(BannerEntity bannerEntity) throws Exception {
        int i = bannerMapper.updateByPrimaryKeySelective(bannerEntity);
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }

    /**
     * 首页管理列表新增
     *
     * @param bannerEntity
     * @return
     * @throws Exception
     */
    public ResponseResult insertBanner(BannerEntity bannerEntity) throws Exception {
        String id = IdUtil.simpleUUID();
        bannerEntity.setId(id);
        bannerEntity.setCreateDate(new Date());
        int i = bannerMapper.insertSelective(bannerEntity);
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }

    /**
     * 首页管理列表删除
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult delBanner(String id) throws Exception {
        int i = bannerMapper.deleteByPrimaryKey(id);
        if(i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }
}
